package com.suian.spring.thread;

public class T7Interrupt {
    public static void main(String[] args) throws InterruptedException {
        final int[] count = {0};
        Thread T = new Thread(() -> {
            while (true) {
                System.out.println("T执行: " + count[0]);
                count[0]++;
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    System.out.println("基于interrupt打断");
                    return;
                }
            }

        });

        T.start();
        Thread.sleep(5000);
        T.interrupt();

        /*结果
         * T执行: 3
         * T执行: 4
         * java.lang.InterruptedException: sleep interrupted
         * 	at java.base/java.lang.Thread.sleep0(Native Method)
         * 	at java.base/java.lang.Thread.sleep(Thread.java:509)
         * 	at com.suian.spring.thread.T7Interrupt.lambda$main$0(T7Interrupt.java:11)
         * 	at java.base/java.lang.Thread.run(Thread.java:1583)
         * 基于interrupt打断
         */
    }
}
